package tool;

import heartBeat.MasterHeartBeatApiInterface;

import java.net.UnknownHostException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

import dataStore.FtpServerApiInterface;
import dataStore.SlaverDataStoreApiInterface;

public class GetMasterApi {
	public static MasterHeartBeatApiInterface get_masterHBApi(Properties slaverProper) 
			throws NumberFormatException, RemoteException, NotBoundException, UnknownHostException {
		try{
			
		final Registry registry = LocateRegistry.getRegistry(
				slaverProper.getProperty("masterAdress"),
				Short.parseShort(slaverProper.getProperty("rmi_masterHeartBeatPort")));
		final MasterHeartBeatApiInterface masterHBApi = (MasterHeartBeatApiInterface) registry
				.lookup("masterHeartBeatApi");

		return masterHBApi;
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				System.out.println("heart beat registory remote e");
				e.printStackTrace();
				
				System.out.println("heart beat registory  failed");
				return null;
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				System.out.println("heart beat registory not bound e");
				e.printStackTrace();
				
				System.out.println("heart beat registory  failed");
				return null;
			}
	}
	
	public static SlaverDataStoreApiInterface get_masterDSApi(Properties slaverProper) 
			throws NumberFormatException, RemoteException, NotBoundException, UnknownHostException {
		try{
			
		final Registry registry = LocateRegistry.getRegistry(
				slaverProper.getProperty("masterAdress"),
				Short.parseShort(slaverProper.getProperty("rmi_masterDataStoreSlaverPort")));
		final SlaverDataStoreApiInterface masterDSApi = (SlaverDataStoreApiInterface) registry
				.lookup("slaverDataStoreApi");
		return masterDSApi;
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				System.out.println("slaver slaverDataStoreApi registory remote erro");
				e.printStackTrace();
				
				System.out.println("slaver slaverDataStoreApi registory failed");
				return null;
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				System.out.println("slaver slaverDataStoreApi registory not boune erro");
				e.printStackTrace();
				
				System.out.println("slaver slaverDataStoreApi registory failed");
				return null;
			}
	}
	
	public static FtpServerApiInterface get_ftpServerApi(Properties slaverProper) 
			throws NumberFormatException, RemoteException, NotBoundException, UnknownHostException {
		try{
			
		final Registry registry = LocateRegistry.getRegistry(
				slaverProper.getProperty("ftpServerAdress"),
				Short.parseShort(slaverProper.getProperty("rmi_ftpServerPort")));
		final FtpServerApiInterface ftpServerApi = (FtpServerApiInterface) registry
				.lookup("ftpServerApi");
		return ftpServerApi ;
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				System.out.println("ftpServerApi  registory remote erro");
				e.printStackTrace();
				
				System.out.println("ftpServerApi  registory failed");
				return null;
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				System.out.println("ftpServerApi registory not boune erro");
				e.printStackTrace();
				
				System.out.println("ftpServerApi  registory failed");
				return null;
			}
	}
}
